package org.leo.flow.step;


import org.leo.flow.bean.InData;
import org.leo.flow.bean.OutData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Created on 2017年2月9日
 * <p>
 * Description 步骤处理类
 * <p>
 * Copyright xxx (c) 2019 .
 *
 * @author leo
 */
public class StepHandler {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	private OutData<String, Object> currentOut;

	private IStep step;

	public IStep getStep() {
		return step;
	}

	public void setStep(IStep step) {
		this.step = step;
	}

	public OutData<String, Object> getCurrentOut() {
		return currentOut;
	}

	public void setCurrentOut(OutData<String, Object> currentOut) {
		this.currentOut = currentOut;
	}

	/**
	 * 
	 * Created on 2017年2月13日 .
	 * <p>
	 * Description 同步调用模块核心方法
	 *
	 * @author leo
	 * @param data
	 * @return SOut<String,String>
	 * @throws Exception
	 */
	public OutData<String, Object> handleStep(InData<String, Object> data) throws Exception {
		OutData<String, Object> allOut = null;

		long startTime = System.currentTimeMillis();
		OutData<String, Object> out = this.getStep().handle(data);
		long interval = System.currentTimeMillis() - startTime;
		log.info("步骤--" + this.getStep().getName() + "执行结束... 花费" + interval + "ms");

		this.setCurrentOut(out);
		// 获取当前步骤的下一步
		IStep nextStep = this.getStep().getNextStep();
		
		if (nextStep != null && "1".equals(out.getResultCode())) {
			// 为下一步骤注入流程对象
			nextStep.setFlow(this.getStep().getFlow());
			// 赋值下一步骤为当期步骤
			this.setStep(nextStep);
			allOut = this.handleStep(out.getData());
		} else {
			allOut = out;
		}
		return allOut;
	}
}
